package com.controller;

import com.entity.pojo.Article;
import com.entity.pojo.ArticleType;
import com.entity.vo.AjaxMassage;
import com.entity.vo.ArticlePage;
import com.service.ArticleService;
import com.service.ArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("select")
public class ArticleController {
    //dasdasdasdasda
    @Autowired
    ArticleService articleService;

    @Autowired
    private ArticleTypeService articleTypeService;
    //查询全部
    @RequestMapping("/wenzhang")
    public String getall( @RequestParam(value = "newsState",required = false,defaultValue = "2") Byte newsState ,
                          @RequestParam(value = "newsTitle",required = false,defaultValue = "")String newsTitle,
                          @RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage,
                          @RequestParam(value = "rows",required = false,defaultValue = "3") Integer rows,
                          HttpSession session,Model model){

        ArticlePage page = articleService.findByPage(currentPage, rows,newsTitle,newsState);
        model.addAttribute("page",page);
        model.addAttribute("newsTitle",newsTitle);
        model.addAttribute("newsState",newsState);
        return "list";
    }
    //返回首页
    @RequestMapping("/outlogin")
    public String outlogin(){
        return "main";
    }

    //跳转到add页面
    @RequestMapping("/toadd")
    public String toadd(Model model){
        List<ArticleType> types = articleTypeService.findType();
        model.addAttribute("types",types);
        return "add";
    }

    //添加页面判断文章标题是否存在
    @GetMapping("/check_is_exist")
    @ResponseBody
    public boolean CheckIsExist(String title){
        boolean flag=false;
        System.out.println("title:"+title);
        Article article = articleService.selecOneByTitle(title);
        if(article==null){
            flag=true;
        }
        System.out.println(flag);
        return flag;
    }
    //添加记录
    @PostMapping ("/addAjax")
    @ResponseBody
    public AjaxMassage addAjax(@Valid Article article, BindingResult bindingResult,
                               @RequestParam(value = "file",required = false) MultipartFile multipartFile,
                               HttpServletRequest request,HttpSession session) {
        System.out.println(article);
        if(bindingResult.hasErrors()){
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            return new AjaxMassage().setCode(100).setSuccess(false).setMassage("验证问题").add("errors",fieldErrors);
        }
        return articleService.addAjax(article,multipartFile,request,session);
    }
    //删除
    @RequestMapping("/del/{newsId}/{currentPage}/{rows}")
    public String del(@PathVariable("newsId") String newsId,@PathVariable("currentPage") Integer currentPage,
                      @PathVariable("rows") Integer rows){
        System.out.println(newsId);
        boolean b = articleService.deleteByPrimaryKey(newsId);
        return "redirect:/select/wenzhang?currentPage="+currentPage+"&rows="+rows;
    }

    //详情页
    @RequestMapping("/selectXQ")
    public String selectXQ(String newsId,Model model){
        Article articles = articleService.selectByPrimaryKey(newsId);
        model.addAttribute("list",articles);
        System.out.println("查询到的值为:" + articles);
        return "xiangqing";
    }
    //修改页
    @RequestMapping("/toupdate")
    public String toupdate(String newsId,Model model){
        Article article = articleService.selectByPrimaryKey(newsId);
        List<ArticleType> types = articleTypeService.findType();
        model.addAttribute("types",types);
        model.addAttribute("list",article);
        return "update";
    }
    //修改操作
    @RequestMapping("/update")
    @ResponseBody
    public AjaxMassage update(@Valid Article article, BindingResult bindingResult,
                               @RequestParam(value = "file",required = false) MultipartFile multipartFile,
                                HttpServletRequest request,HttpSession session) {
                        if(bindingResult.hasErrors()){
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            return new AjaxMassage().setCode(100).setSuccess(false).setMassage("验证问题").add("errors",fieldErrors);
        }
        return articleService.update(article,multipartFile,request,session);
    }
}
